package com.shameit.goodqrinfomanager.ui.activity.main;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.mylibrary.log.LogUtils;
import com.shameit.goodqrinfomanager.AppViewModelFactory;
import com.shameit.goodqrinfomanager.BR;
import com.shameit.goodqrinfomanager.BaseMVVMActivity;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.adapter.MenuAdapter;
import com.shameit.goodqrinfomanager.bean.enity.MenuBean;
import com.shameit.goodqrinfomanager.bean.litepal.CangKu;
import com.shameit.goodqrinfomanager.bean.litepal.CangWei;
import com.shameit.goodqrinfomanager.bean.litepal.ChanPin;
import com.shameit.goodqrinfomanager.databinding.ActivityMainBinding;
import com.shameit.goodqrinfomanager.ui.activity.actionlog.ActionLogActivity;
import com.shameit.goodqrinfomanager.ui.activity.barcode.BarCodeManagementActivity;
import com.shameit.goodqrinfomanager.ui.activity.cangku.CangkuManagementActivity;
import com.shameit.goodqrinfomanager.ui.activity.query.InteQueryActivity;
import com.shameit.goodqrinfomanager.ui.activity.setting.BaseicSettingActivity;
import com.shameit.goodqrinfomanager.ui.activity.sync.DataSyncActivity;
import com.sunfusheng.transformation.RadiusTransformation;

import org.litepal.LitePal;

import java.util.ArrayList;

import me.goldze.mvvmhabit.utils.ToastUtils;


public class MainActivity extends BaseMVVMActivity<ActivityMainBinding, MainViewModel> {
    private static long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String uername = getIntent().getStringExtra("username");
        viewModel.username.set(uername);
    }

    @Override
    protected String setTitle() {
        return "";
    }

    @Override
    protected void initView() {
        binding.gvAvatar.loadDrawable(R.mipmap.avatar_url);
        binding.topBg.load(R.mipmap.top_bg_url, 0, new RadiusTransformation(this, 10));
        binding.gvSmallAvatar.loadDrawable(R.mipmap.small_avatar_url);

        binding.rvMenus.setLayoutManager(new GridLayoutManager(this, 4));
        binding.rvMenus.setAdapter(viewModel.adapter = new MenuAdapter(R.layout.item_menu));
        ArrayList arrayList = new ArrayList();
        int[] res = {R.mipmap.icons1, R.mipmap.icons2, R.mipmap.icons3, R.mipmap.icons4, R.mipmap.icons5, R.mipmap.icons6, R.mipmap.icons7, R.mipmap.icons8};
        for (int i = 0; i < 8; i++) {
            arrayList.add(new MenuBean(res[i], getResources().getStringArray(R.array.icons_name)[i]));
        }
        viewModel.adapter.setNewInstance(arrayList);


        viewModel.adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                switch (position) {
                    case 0:
                        Intent barCode = new Intent(MainActivity.this, BarCodeManagementActivity.class);
                        startActivity(barCode);
                        break;
                    case 1:
                        Intent cangku = new Intent(MainActivity.this, CangkuManagementActivity.class);
                        startActivity(cangku);
                        break;
                    case 3:
                        Intent intent = new Intent(MainActivity.this, ActionLogActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        Intent intent2 = new Intent(MainActivity.this, BaseicSettingActivity.class);
                        startActivity(intent2);
                        break;
                    case 4:
                        Intent dataSync = new Intent(MainActivity.this, DataSyncActivity.class);
                        startActivity(dataSync);
                        break;
                    case 6:
                        Intent query = new Intent(MainActivity.this, InteQueryActivity.class);
                        startActivity(query);

                        break;
                }
            }
        });
    }

    @Override
    protected void loadData() {
        SQLiteDatabase db = LitePal.getDatabase();
        LogUtils.d(LitePal.getDatabase().getPath());
    }


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.data;
    }

    @Override
    public MainViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MainViewModel.class);
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            //Toast.makeText(this, getString(R.string.activity_main_exit_hint), Toast.LENGTH_SHORT).show();
            ToastUtils.showShort(getApplication().getString(R.string.activity_main_exit_hint));
            mExitTime = System.currentTimeMillis();
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
            System.exit(0);
        }
    }

}